<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <title>欢迎页面-X-admin2.0</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi"/>
    <link rel="shortcut icon" href="/favicon.ico" type="image/x-icon"/>
    <link rel="stylesheet" href="./css/font.css">
    <link rel="stylesheet" href="./css/xadmin.css">
    <script>
        Date.prototype.Format = function (fmt) {
            var hours = this.getHours() + 8;
            if (hours > 24) {
                hours = hours - 24;
            }
            if (hours == 24) {
                hours = 0;
            }
            var o = {
                "M+": this.getMonth() + 1, //月份
                "d+": this.getDate(), //日
                "H+": hours, //小时
                "m+": this.getMinutes(), //分
                "s+": this.getSeconds(), //秒
                "q+": Math.floor((this.getMonth() + 3) / 3), //季度
                "S": this.getMilliseconds() //毫秒
            };

            if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
            for (var k in o)
                if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
            return fmt;
        };
    </script>
    <script type="text/javascript" src="./js/jquery.min.js"></script><script src="./js/common.js"></script>
    <script type="text/javascript" src="./lib/layui/layui.js" charset="utf-8"></script>
    <script type="text/javascript" src="./js/xadmin.js"></script>
    <!-- 让IE8/9支持媒体查询，从而兼容栅格 -->
    <!--[if lt IE 9]>
    <script src="./js/html5.min.js"></script>
    <script src="./js/respond.min.js"></script>
    <![endif]-->
    <script>
        var baseUrl = parent.baseUrl;
        var env = $.getUrlParam("env");
        var gateway = $.getUrlParam("gateway");
        var likeId = $.getUrlParam("likeId");
        if (!likeId) {
            likeId = "";
        }
        var likePath = $.getUrlParam("likePath");
        if (!likePath) {
            likePath = "";
        }
        var likeServiceId = $.getUrlParam("likeServiceId");
        if (!likeServiceId) {
            likeServiceId = "";
        }
        var likeDomain = $.getUrlParam("likeDomain");
        if (!likeDomain) {
            likeDomain = "";
        }

        var likeComment = $.getUrlParam("likeComment");
        if (!likeComment) {
            likeComment = "";
        } else {
            likeComment = encodeURI(likeComment);
        }

        $(function () {
            $("#l_like_id").val(decodeURI(decodeURI(likeId)));
            $("#l_like_path").val(decodeURI(decodeURI(likePath)));
            $("#l_like_service_id").val(decodeURI(decodeURI(likeServiceId)));
            $("#l_like_domain").val(decodeURI(decodeURI(likeDomain)));
            $("#l_like_comment").val(decodeURI(decodeURI(likeComment)));
        });
    </script>
</head>

<body class="layui-anim layui-anim-up">
<div class="x-nav">
    <a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right"
       href="javascript:location.replace(location.href);" title="刷新">
        <i class="layui-icon" style="line-height:30px">ဂ</i></a>
</div>
<div class="x-body">
    <div class="layui-row">
        <form class="layui-form layui-col-md12 x-so">
            <input id="l_like_id" type="text" name="likeId" placeholder="请输入ID" autocomplete="off"
                   class="layui-input"/>
            <input id="l_like_path" type="text" name="likePath" placeholder="请输入路径" autocomplete="off"
                   class="layui-input"/>
            <input id="l_like_service_id" type="text" name="likeServiceId" placeholder="请输入组ID" autocomplete="off"
                   class="layui-input"/>
            <input id="l_like_domain" type="text" name="likeDomain" placeholder="请输入域名" autocomplete="off"
                   class="layui-input"/>
            <input id="l_like_comment" type="text" name="likeComment" placeholder="请输入备注" autocomplete="off"
                   class="layui-input"/>
            <button class="layui-btn" lay-submit="" lay-filter="sreach"><i class="layui-icon">&#xe615;</i></button>
        </form>
    </div>
    <xblock>
        <button class="layui-btn" onclick="openRouteAdd()"><i class="layui-icon"></i>添加
        </button>
    </xblock>
    <table class="layui-table">
        <thead>
        <tr>
            <th>ID</th>
            <th>路径</th>
            <th>组ID</th>
            <th>域名</th>
            <th>去除路径前缀</th>
            <!--<th>重试</th>-->
            <!--<th>自定义敏感头信息</th>-->
            <th>敏感头信息内容</th>
            <th>添加真实访问来源HOST</th>
            <th>排序</th>
            <th>备注</th>
            <th>创建时间</th>
            <th>状态</th>
            <th>版本路由设置</th>
            <th>操作</th>
        </tr>
        </thead>
        <tbody id="routeListView">

        </tbody>
    </table>

</div>
<script>

    function openRouteAdd() {
        var id = "rut" + new Date().Format("yyyyMMddHHmmssS");
        x_admin_show('添加路由', './gateway-route-add.html?env=' + env + "&gateway=" + gateway + "&id=" + id, 900, 600);
    }

    function openRouteVersionSetting(id) {
        x_admin_show('路由版本管理', './gateway-route-version-list.html?env=' + env + "&gateway=" + gateway + "&id=" + id);
    }
</script>

<script>


    function openRouteEditWin(id) {
        x_admin_show('编辑路由', './gateway-route-edit.html?env=' + env + "&gateway=" + gateway + "&id=" + id, 900, 600);
    }

    /*-停用*/
    function route_stop(obj, id) {
        var msg = "可用";
        var newStatus = 1;

        if ($(obj).attr('title') == '可用') {
            msg = "不可用";
            newStatus = 0;
        }


        layer.confirm('确认要' + msg + '吗,相关的版本路由也会被' + msg + '？', function (index) {

            var reqData = {
                request: {
                    status: newStatus, id: id
                }
            };

            $.ajax({
                url: baseUrl + "/servcieGateway/route/update?env=" + env + "&gateway=" + gateway,
                data: JSON.stringify(reqData),
                contentType: 'application/json;charset=utf-8',
                dataType: "JSON",
                async: false,
                type: "POST",
                success: function (respData) {
                    if (respData.respCode === '0000') {
                        if ($(obj).attr('title') == '可用') {

                            //发异步把用户状态进行更改
                            $(obj).attr('title', '不可用')
                            $(obj).find('i').html('&#xe62f;');

                            $(obj).parents("tr").find(".td-status").find('span').addClass('layui-btn-disabled').html('不可用');
                            layer.msg('不可用!', {icon: 4, time: 1000});

                        } else {
                            $(obj).attr('title', '可用')
                            $(obj).find('i').html('&#xe601;');

                            $(obj).parents("tr").find(".td-status").find('span').removeClass('layui-btn-disabled').addClass('layui-btn-normal').html('可用');
                            layer.msg('可用!', {icon: 6, time: 1000});
                        }
                    } else {
                        layer.msg(respData.respDesc, {
                            icon: 2,
                            time: 2000 //2秒关闭（如果不配置，默认是3秒）
                        }, function () {
                            //do something
                        });
                    }
                }
            });

        });
    }

    /*-删除*/
    function route_del(obj, id) {
        layer.confirm('确认要删除[' + id + ']吗,会关联删除其下的版本配置？', function (index) {
            $(function () {
                $.ajax({
                    url: baseUrl + "/servcieGateway/route/delete/" + id + "/?env=" + env + "&gateway=" + gateway,
                    data: "",
                    contentType: 'application/json;charset=utf-8',
                    dataType: "JSON",
                    async: false,
                    type: "GET",
                    success: function (respData) {
                        if (respData.respCode === '0000') {
                            //发异步删除数据
                            $(obj).parents("tr").remove();
                            layer.msg('已删除!', {icon: 1, time: 1000});

                        } else {
                            layer.msg(respData.respDesc, {
                                icon: 2,
                                time: 2000 //2秒关闭（如果不配置，默认是3秒）
                            }, function () {
                                //do something
                            });
                        }
                    }
                });

            });

        });
    }

</script>


<script>
    layui.use(['laytpl', 'layer'], function () {
        var laytpl = layui.laytpl;
        var layer = layui.layer;

        var likeComment = $.getUrlParam("likeComment");
        if (!likeComment) {
            likeComment = "";
        } else {
            likeComment = encodeURI(likeComment);
        }

        $.ajax({
            url: baseUrl + "/servcieGateway/route/list?likeComment=" + likeComment + "&env=" + env + "&gateway=" + gateway + "&applicationName=" + gateway + "&likeId=" + likeId + "&likePath=" + likePath + "&likeServiceId=" + likeServiceId + "&likeDomain=" + likeDomain,
            data: "",
            contentType: 'application/json;charset=utf-8',
            dataType: "JSON",
            // async: false,
            type: "GET",
            timeout: 10000,
            success: function (respData) {
                if (respData.respCode === '0000') {
                    var data = {
                        "list": respData.data.items
                    };

                    var getTpl = routeListViewScript.innerHTML
                        , view = document.getElementById('routeListView');
                    laytpl(getTpl).render(data, function (html) {
                        view.innerHTML = html;
                    });
                } else {
                    layer.msg(respData.respDesc, {
                        icon: 2,
                        time: 2000 //2秒关闭（如果不配置，默认是3秒）
                    }, function () {
                        //do something
                    });
                }
            }
        });
    });
</script>

<script id="routeListViewScript" type="text/html">
    {{# layui.each(d.list, function(index, item){ }}

    <tr>

        <td>{{ item.id }}</td>
        <td>{{ item.path }}</td>
        <td>{{ item.routeService.id }} <br/> <small>备注：{{ item.routeService.comment }}</small></td>

        <td>{{ item.routeDomain.domain }}</td>
        <td>{{ item.stripPrefix }}</td>
        <!--<td>{{ item.retryable }}</td>-->
        <!--<td>{{ item.customSensitiveHeaders }}</td>-->
        <td>{{ item.sensitiveHeaders }}</td>
        <td>{{ item.addHostHeader }}</td>
        <td>{{ item.sort }}</td>

        <td>{{ item.comment }}</td>
        <td>{{ item.createTime }}</td>

        {{# if(item.statusCode === 0){ }}
        <td class="td-status">
            <span class="layui-btn layui-btn-disabled layui-btn-sm">{{ item.statusDesc }}</span>
        </td>
        {{# } }}


        {{# if(item.statusCode === 1){ }}
        <td class="td-status">
            <span class="layui-btn layui-btn-normal layui-btn-sm">{{ item.statusDesc }}</span>
        </td>
        {{# } }}


        {{# if(item.routeVersions.length === 0){ }}
        <td>
            <span class="layui-btn layui-btn-primary layui-btn-sm" onclick="openRouteVersionSetting('{{ item.id }}')"><i
                    class="layui-icon layui-icon-set-sm">&#xe620;</i>设置
            </span>
        </td>
        {{# } }}


        {{# if(item.routeVersions.length > 0){ }}
        <td>
            <span class="layui-btn layui-btn-normal layui-btn-sm" onclick="openRouteVersionSetting('{{ item.id }}')"><i
                    class="layui-icon layui-icon-set-sm">&#xe60b;</i>详情</span>
        </td>
        {{# } }}


        <td class="td-manage">

            {{# if(item.statusCode === 1){ }}
            <a onclick="route_stop(this,'{{ item.id }}')" href="javascript:;" title="可用">
                <i class="layui-icon">&#xe601;</i>
            </a>
            {{# } }}

            {{# if(item.statusCode === 0){ }}
            <a onclick="route_stop(this,'{{ item.id }}')" href="javascript:;" title="不可用">
                <i class="layui-icon">&#xe62f;</i>
            </a>
            {{# } }}


            <a title="编辑"
               onclick="openRouteEditWin('{{ item.id }}')"
               href="javascript:;">
                <i class="layui-icon">&#xe642;</i>
            </a>
            <a title="删除" onclick="route_del(this,'{{ item.id }}')" href="javascript:;">
                <i class="layui-icon">&#xe640;</i>
            </a>
        </td>
    </tr>

    {{# }); }}
</script>


<script>
    layui.use(['form', 'layer'], function () {
        $ = layui.jquery;
        var form = layui.form
            , layer = layui.layer;

        //监听提交
        form.on('submit(sreach)', function (data) {


            var likeId = $.trim(data.field.likeId);
            if (!likeId) {
                likeId = "";
            }
            var likePath = $.trim(data.field.likePath);
            if (!likePath) {
                likePath = "";
            }
            var likeServiceId = $.trim(data.field.likeServiceId);
            if (!likeServiceId) {
                likeServiceId = "";
            }
            var likeDomain = $.trim(data.field.likeDomain);
            if (!likeDomain) {
                likeDomain = "";
            }

            var likeComment = $.trim(data.field.likeComment);
            if (likeComment) {
                likeComment = encodeURI(encodeURI(likeComment));
            }

            var url = "./gateway-route-list.html?likeComment=" + likeComment + "&env=" + env + "&gateway=" + gateway + "&applicationName=" + gateway + "&likeId=" + likeId + "&likePath=" + likePath + "&likeServiceId=" + likeServiceId + "&likeDomain=" + likeDomain;
            window.location.href = url;
            return false;
        });

    });
</script>

<script>var _hmt = _hmt || [];
(function () {
    var hm = document.createElement("script");
    hm.src = "https://hm.baidu.com/hm.js?b393d153aeb26b46e9431fabaf0f6190";
    var s = document.getElementsByTagName("script")[0];
    s.parentNode.insertBefore(hm, s);
})();</script>
</body>

</html>